#include <pthread.h>
#include <iostream>
#include <string>
#include <unistd.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
void *Wait(void *args)
{
    const char *name = (char *)args;
    while (true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        usleep(10000);
        std::cout << name << " is running" << std::endl;
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    pthread_t threads[10];
    for (int i = 0; i < 10; i++)
    {
        char* name=new char[128];
        snprintf(name,128,"i am thread %d",i+1);
        pthread_create(threads + i, nullptr, Wait, (void *)name);
        // sleep(1);
    }
    //唤醒所有线程
    while(true)
    {
        pthread_cond_signal(&cond);
        std::cout<<"唤醒一个线程"<<std::endl;
        usleep(10000);
    }
    for (int i = 0; i < 10; i++)
    {
        pthread_join(*(threads + i), nullptr);
        std::cout <<"thread "<<i<< " excute done " << std::endl;
    }
    return 0;
}